import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

public class AddHandler implements LoginHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studentId = request.getParameter("student_id");
        String studentName = new String(request.getParameter("student_name").getBytes("ISO-8859-1"), "UTF-8");
        String studentAge = request.getParameter("student_age");

        HttpSession session = request.getSession();
        ArrayList<String> studentInfo = (ArrayList<String>) session.getAttribute("studentInfo");

        if (studentInfo == null) {
            // 如果会话中不存在学生信息列表，则创建一个新的列表
            studentInfo = new ArrayList<>();
        }

        // 将新的学生信息添加到列表中
        studentInfo.add(studentId);
        studentInfo.add(studentName);
        studentInfo.add(studentAge);

        // 将更新后的学生信息列表重新设置到会话中
        session.setAttribute("studentInfo", studentInfo);

        response.sendRedirect(request.getContextPath() + "/welcome.jsp");
    }
}
